<template>
  <el-container v-loading="loading">

    <el-row>
      <el-button
        icon="fa fa-fw fa-refresh"
        size="mini"
        type="info"
        @click="handleRefresh">
      </el-button>

      <div class="autocomplete-group">
        <span>服务人员查询：</span>
        <el-autocomplete
          v-model="constraintForm.text"
          :fetch-suggestions="querySearchAsync"
          placeholder="请选择服务人员"
          @select="handleSelect"
          clearable
          size="small">
          <template slot-scope="{ item }">
            <div>{{ item.real_name }}</div>
          </template>
          <span slot="append">当前选择服务人员：{{constraintForm.text == '' ? '暂无' : constraintForm.text}}</span>
        </el-autocomplete>
      </div>
    </el-row>

    <el-table
      @selection-change="handleSelectionChange"
      :data="tableData"
      tooltip-effect="dark"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      :row-class-name="tableRowClassName"
      size="small">
      <el-table-column
        prop="order_no"
        label="订单号"
        width="250">
      </el-table-column>

      <el-table-column
        prop="passenger_name"
        label="乘车人姓名"
        width="180">
      </el-table-column>

      <el-table-column
        prop="passenger_phone"
        label="乘车人电话"
        width="200">
      </el-table-column>

      <el-table-column
        prop="start_adds"
        label="起点"
        width="300">
      </el-table-column>

      <el-table-column
        prop="target_adds"
        label="终点"
        width="300">
      </el-table-column>

      <el-table-column
        prop="create_time"
        label="创建时间"
        width="180">
        <template slot-scope="scope">
          <i class="fa fa-clock-o fa-fw"></i>
          <span style="margin-left: 10px">{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        fixed="right"
        width="180">
        <template slot-scope="scope" class="operate-column">
          <el-button
            icon="fa fa-fw fa-handshake-o"
            size="mini"
            type="primary"
            title="订单分配"
            @click="taskAppointItem(scope.$index, scope.row)">
            订单分配
          </el-button>
        </template>
      </el-table-column>
    </el-table>


    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-sizes="pagination.pageSizes"
      :page-size="pagination.pageSize"
      :total="pagination.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

  </el-container>
</template>

<script>
  export default {
    mounted: function () {
      this.getData();
    },
    methods: {
      /**
       * 数据请求
       * */
      getData: function () {
        let _this = this;
        this.api.getOrderTaskingList({
          page: this.pagination.currentPage,
          rows: this.pagination.pageSize
        }).then(function (res) {
          if (res.code == 1) {
            _this.tableData = res.data.data;
            _this.pagination.total = res.data.total;
          }
          _this.loading = false;
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 分页数改变
       */
      handleSizeChange: function (pageSize) {
        this.pagination.pageSize = pageSize;
        this.getData();
      },
      /**
       * 当前页面改变
       * @param currentPage
       */
      handleCurrentChange: function (currentPage) {
        this.pagination.currentPage = currentPage;
        this.getData();
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      /**
       * 编辑单个
       * */
      taskAppointItem(index, row) {
        let _this = this;
        let _id = row.id;
        if(_this.constraintForm.text == "" || _this.constraintForm.id == ''){
          this.$message({
            type: 'warning',
            message: '请选在上方进行筛选需要派单的服务人员'
          })
          return;
        }
        this.$confirm('此操作将订单分派给: '+ _this.constraintForm.text +', 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.api.taskAppoint({
            store_id: _this.constraintForm.store_id,
            id: _id,
            did: _this.constraintForm.id
          }).then(function (res) {
            if (res.code == 1) {
              _this.getData();
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'success',
              });
            } else {
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          }).catch(function (res) {
            throw new Error(res);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 数据刷新
       */
      handleRefresh: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '刷新成功',
          type: 'success'
        });
      },
      /**
       * 表格选中状态记录
       * @param val
       */
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      /**
       * 条件数据检索
       */
      constraintSearchData: function () {
        this.getData();
        this.$message({
          showClose: true,
          message: '查询成功',
          type: 'success'
        });
      },
      /**
       * 远程数据加载
       */
      querySearchAsync(queryString, cb) {
        let _this = this;
        this.api.getDrivingByTask().then(function (res) {
          if (res.code == '1') {
            cb(res.data);
          }
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 搜索下拉选择
       * @param item
       */
      handleSelect(item) {
        this.constraintForm.text = item.real_name;
        this.constraintForm.id = item.id;
        this.constraintForm.store_id = item.store_id;
      },
    },
    data() {
      return {
        loading: true,  // 页面加载
        tableData: [],
        multipleSelection: [],
        pagination: {
          total: 5,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [5, 10, 20, 25, 30, 50, 100],
        },
        constraintForm: {
          text: '',    // 检索姓名，默认''
          id: '',    // 用户id，默认0
          store_id: '',
        }
      }
    },
  }
</script>

<style scoped>
  .el-container {
    display: block;
  }

  .el-table, .el-row {
    width: 98% !important;
    margin: 15px auto;
  }

  .el-pagination {
    text-align: center;
    margin: 15px 0;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .logo-img {
    width: 40px;
    height: 40px;
  }

  .autocomplete-group{
    display: inline-block;
    margin-left: 15px;
    font-size: 12px;
  }
</style>
